package com.mysoft.gfyx.workbench.controller;

import com.mysoft.framework.cache.api.CacheProvider;
import com.mysoft.framework.context.api.ContextManager;
import com.mysoft.framework.rpc.annotation.PubAction;
import com.mysoft.framework.rpc.annotation.PubService;
import com.mysoft.framework.rpc.contants.RequestPrefix;
import com.mysoft.gfyx.workbench.controller.dto.GetSupplierUserSsoDto;
import com.mysoft.gfyx.workbench.service.YcgService;
import com.mysoft.gfyx.workbench.service.dto.UserBusinessOpportunityRequestDto;
import com.mysoft.gfyx.workbench.service.dto.UserGetTokenRequestDto;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;

@PubService(value = "/ycgDataApi", prefix = RequestPrefix.API)
@Tag(name = "指标控制器-API")
public class YcgDataApiController {

    @Resource
    private YcgService ycgService;

    @Resource
    private ContextManager contextManager;

    @Resource
    private CacheProvider cacheProvider;

    @Autowired
    private Environment environment;

    @PubAction(value = "/getUserToken", method = RequestMethod.POST)
    public Object getUserToken() {
        // 获取当前用户的手机号
        String mobile = contextManager.fetchContextInfo().getUserContext().getMobilePhone();
        if (mobile == null || mobile.length() == 0) {
            mobile = contextManager.fetchContextInfo().getUserContext().getUserCode();
        }
        // String mobile = "13579246839";
        String cacheKey = "WORKBENCH:YCG:USER_TOKEN:" + mobile;
        Object cacheResponse = cacheProvider.get(cacheKey);
        if (cacheResponse != null) {
            // return cacheResponse;
        }

        // 获取租户库数据
        String tenantCode = contextManager.fetchContextInfo().getTenantCode();
        // 调用 token 方法
        UserGetTokenRequestDto requestDto = new UserGetTokenRequestDto();
        requestDto.setMobile(mobile);
        requestDto.setSaasCode("gfyx");
        requestDto.setTenantCode(tenantCode);
        Object response = ycgService.getSupplierUserAuthToken(requestDto);
        // cacheProvider.set(cacheKey, response, 7200);
        // 缓存数据
        // YcgUserGetTokenResponseDto responseDto = new YcgUserGetTokenResponseDto();
        // responseDto.setToken("8bfba549-0cbe-11ed-aa7b-043f72ebdebc");
        return response;
    }

    @PubAction(value = "/getUserBusinessOpportunity", method = RequestMethod.POST)
    public Object YcgUserBusinessOpportunity(@RequestBody UserBusinessOpportunityRequestDto requestDto) {
        Object responseDto = ycgService.getUserBusinessOpportunity(requestDto);
        return responseDto;
    }

    @PubAction(value = "/getSupplierUserSsoUrl", method = RequestMethod.POST)
    public Object YcgSupplierUserSso(@RequestBody GetSupplierUserSsoDto requestDto) {
        String url = "";
        if (StringUtils.isNotBlank(requestDto.getTargetUrlCode())) {
            // 往 site 取数 "https://gys.b2btst.com/newgys/#/business";
            url = environment.getProperty("ycg.gys-domain") + "/newgys/#/business";
        } else {
            url = requestDto.getDetailUrl();
        }

        UserGetTokenRequestDto getTokenRequestDto = new UserGetTokenRequestDto();
        getTokenRequestDto.setMobile(contextManager.fetchContextInfo().getUserContext().getMobilePhone());
        // getTokenRequestDto.setMobile("13579246839");
        getTokenRequestDto.setSaasCode("gfyx");
        getTokenRequestDto.setTenantCode(contextManager.fetchContextInfo().getTenantCode());
        getTokenRequestDto.setTargetUrl(url);

        Object response = ycgService.getSupplierUserSsoUrl(getTokenRequestDto);
        return response;
    }
}
